/*
 * speechManager.cpp
 *
 *  Created on: 2020年9月15日
 *      Author: chencheng
 */
#include "./speechManager.h"

SpeechManager::SpeechManager() {
	this->initSpeech();
	this->createSpeaker();
	this->loadRecord();
}

SpeechManager::~SpeechManager() {

}
//测试用例
void SpeechManager::testShow() {
	for (map<int, Speaker>::iterator it = this->m_Speaker.begin();
			it != this->m_Speaker.end(); it++) {
		cout << "选手编号： " << it->first << "  姓名： " << it->second.m_Name
				<< "  成绩： " << it->second.m_Score[0] << endl;
	}
}
//菜单功能
void SpeechManager::show_Menu() {
	cout << "*******************************************************" << endl;
	cout << "***************\t                      欢迎参加演讲比赛\t***************"
			<< endl;
	cout << "***************\t\t1.开始演讲比赛\t***************" << endl;
	cout << "***************\t\t2.查看往届记录\t***************" << endl;
	cout << "***************\t\t3.清空比赛记录\t***************" << endl;
	cout << "***************\t\t0.退出比赛程序\t***************" << endl;
	cout << "*******************************************************" << endl;
	cout << endl;
}

//初始化属性
void SpeechManager::initSpeech() {
	//容器保证为空
	this->v1.clear();
	this->v2.clear();
	this->vVictory.clear();
	this->m_Speaker.clear();
	this->m_Record.clear();
	//初始化比赛轮数
	this->m_Index = 1;
}

//创建12个 演讲者
void SpeechManager::createSpeaker() {
	string nameSeed = "ABCDEFGHIJKL";
	int strSize = nameSeed.size();
	for (int i = 0; i < strSize; i++) {
		string name = "选手";
		name += nameSeed[i];

		Speaker sp;
		sp.m_Name = name;

		for (int j = 0; j < 2; j++) {
			sp.m_Score[j] = 0;
		}
		//记录12人的编号
		this->v1.push_back(i + 10001);
		//记录编号对应的选手
		this->m_Speaker.insert(make_pair(i + 10001, sp));

	}
}

//开始演讲比赛
void SpeechManager::startSpeech() {
	//第一轮比赛
	//1.抽签
	this->speechDraw();
	//2.比赛
	this->speechContest();
	//3.显示晋级结果
	this->showScore();
	//第二轮比赛
	this->m_Index = 2;
	//1.抽签
	this->speechDraw();
	//2.比赛
	this->speechContest();
	//3.显示最终结果
	this->showScore();
	//4.保存分数
	this->saveRecord();

	cout << "本届比赛完毕！ " << endl;

	this->initSpeech();
	this->createSpeaker();
	this->loadRecord();
}

//抽签
void SpeechManager::speechDraw() {
	//根据比赛到了第一轮，打乱对应保存选手的容器中的数据顺序
	cout
			<< "-----------------------------------------------------------------------"
			<< endl;
	cout << "第 << " << this->m_Index << ">> 轮比赛选手正在抽签 " << endl;
	cout << "抽签后演讲顺序如下： " << endl;
	if (this->m_Index == 1) {
		random_shuffle(v1.begin(), v1.end());

		for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) {
			cout << *it << " ";
		}

	} else {

		random_shuffle(v2.begin(), v2.end());

		for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++) {
			cout << *it << " ";
		}

	}
	cout << endl;
	cout
			<< "-----------------------------------------------------------------------"
			<< endl;

}

//比赛
void SpeechManager::speechContest() {
	cout << "------------	第  " << this->m_Index << " 轮正式比赛开始：	------------ "
			<< endl;

	multimap<double, int, greater<double>> groupScore; //临时容器，保存key分数 value 选手编号
	int num = 0; //记录人员数 6个为一组

	vector<int> v_Src; //比赛的人员容器

	if (this->m_Index == 1) {
		v_Src = v1;
	} else if (this->m_Index == 2) {
		v_Src = v2;
	} else {
		cout << "程序发生异常！" << endl;
		return;
	}

	//评委打分
	for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++) {
		num++;

		//记录10个评委打的分数，正对某个参赛选手
		deque<double> d;
		for (int i = 0; i < 10; i++) {
			double score = (rand() % 401 + 600) / 10.0f;
			d.push_back(score);

		}

		sort(d.begin(), d.end(), greater<double>());  //排序
		//去除最低分和最高分
		d.pop_front();
		d.pop_back();

		//取平均分
		double sum = accumulate(d.begin(), d.end(), 0.0f);
		double avg = sum / (double) (d.size());

		//打印平均分
		//cout << "编号： " << *it << " 姓 名 ： " << this->m_Speaker[*it].m_Name << " 获取平均分： " << avg << endl;

		//记录每个人的分数
		this->m_Speaker[*it].m_Score[this->m_Index - 1] = avg;

		//6个人一组，用临时容器保存
		groupScore.insert(make_pair(avg, *it));

		if (num % 6 == 0) {
			cout << "第 " << num / 6 << " 小组比赛名次： " << endl;
			for (multimap<double, int, greater<double>>::iterator map_it =
					groupScore.begin(); map_it != groupScore.end(); map_it++) {
				cout << "编号： " << map_it->second << " 姓名： "
						<< this->m_Speaker[map_it->second].m_Name << " 成绩： "
						<< this->m_Speaker[map_it->second].m_Score[this->m_Index
								- 1] << endl;
			}

			int count = 0;

			for (multimap<double, int, greater<double>>::iterator map_it =
					groupScore.begin(); map_it != groupScore.end() && count < 3;
					map_it++, count++) {
				if (this->m_Index == 1) {
					this->v2.push_back(map_it->second);
				} else {
					this->vVictory.push_back(map_it->second);
				}

			}

			groupScore.clear();

			cout << endl;
		}

	}

}

//显示比赛结果
void SpeechManager::showScore() {
	cout << "----------第 " << this->m_Index << " 轮晋级选手信息如下:	----------" << endl;
	vector<int> v;
	if (this->m_Index == 1) {
		v = this->v2;

	} else if (this->m_Index == 2) {
		v = this->vVictory;
	} else {
		cout << "程序出现问题！ " << endl;
		return;
	}

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << "选手编号： " << *it << " 姓名： " << this->m_Speaker[*it].m_Name
				<< " 得分： " << this->m_Speaker[*it].m_Score[this->m_Index - 1]
				<< endl;
	}
	cout << endl;

}

//保存记录到文件
void SpeechManager::saveRecord() {
	ofstream ofs;
	ofs.open("speech.csv", ios::out | ios::app); //用输出的方式打开文件 --写文件  追加文本

	//将前三名的数据写入到文件中
	for (vector<int>::iterator it = this->vVictory.begin();
			it != this->vVictory.end(); it++) {
		ofs << *it << "," << this->m_Speaker[*it].m_Score[1] << ",";
	}

	ofs << endl;

	ofs.close();

	cout << "记录已经保存！ " << endl;
	this->fileIsEmpty = false;

}

//读取记录
void SpeechManager::loadRecord() {
	ifstream ifs("speech.csv", ios::in);
	if (!ifs.is_open()) {
		this->fileIsEmpty = true;
		cout << " 文件不存在！ " << endl;
		ifs.close();
		return;
	}
	//打开成功，先判断文件是否有内容
	char ch;
	ifs >> ch;

	if (ifs.eof()) {
		cout << "文件内容为空！  " << endl;
		ifs.close();
		this->fileIsEmpty = true;
		return;
	}

	//文件存在，并且包含内容
	this->fileIsEmpty = false;
	ifs.putback(ch); //之前读了一个字符了，因此需要再读回一个字符

	string data;
	int index = 0;
	while (ifs >> data) { //一行一行的读
		vector<string> v;
		int pos = -1;
		unsigned int start = 0;

		while (true) {
			pos = data.find(",", start);
			if (pos == -1) {
				break;
			}
			string tmp = data.substr(start, pos - start); //找到了，进行分割 参数1 起始位置，参数2  截取长度
			v.push_back(tmp);
			start = pos + 1;
		}
		this->m_Record.insert(make_pair(index, v));
		index++;
	}
	ifs.close();

}

//查看往届记录
void SpeechManager::showRecord() {

	if (this->fileIsEmpty) {
		cout << "往届记录为空！" << endl;

	} else {
		for (map<int, vector<string>>::iterator it = this->m_Record.begin();
				it != this->m_Record.end(); it++) {
			//等效
			// 冠军 this->m_Record[0][0] this->m_Record[0][1]
			// 亚军 this->m_Record[0][2] this->m_Record[0][3]
			cout << "第 " << it->first + 1 << "届  " << "冠军编号： " << it->second[0]
					<< " 分数： " << it->second[1] << " " << "亚军编号： "
					<< it->second[2] << " 分数： " << it->second[3] << " "
					<< "季军编号： " << it->second[4] << " 分数： " << it->second[5]
					<< " " << endl;
		}
	}

}

//清空记录
void SpeechManager::clearRecord() {
	cout << "确认清空？" << endl;
	cout << "1、确认" << endl;
	cout << "2、返回" << endl;

	int select = 0;
	cin >> select;

	if (select == 1) {
		//打开模式， ios::trunc 如果文件存在则删除文件并重新创建
		ofstream ofs("speech.csv", ios::trunc);
		ofs.close();

		//清除
		this->initSpeech();
		this->createSpeaker();
		this->loadRecord();

		cout << "清空成功！ " << endl;
	} else if (select == 2) {
		cout << "退出清空操作！ " << endl;
	}
}

